12#ifndef AOM_AV1_ENCODER_RC_UTILS_H_
13#define AOM_AV1_ENCODER_RC_UTILS_H_
16#include "aom_dsp/psnr.h"
22static inline void check_reset_rc_flag(
AV1_COMP *cpi) {
26 (
unsigned int)cpi->
svc.number_spatial_layers) {
28 av1_svc_check_reset_layer_rc_flag(cpi);
30 if (rc->avg_frame_bandwidth / 3 > (rc->prev_avg_frame_bandwidth >> 1) ||
31 rc->avg_frame_bandwidth < (rc->prev_avg_frame_bandwidth >> 1)) {
53 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
55 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
64static inline void config_target_level(
AV1_COMP *
const cpi,
65 AV1_LEVEL target_level,
int tier) {
68 TileConfig *
const tile_cfg = &oxcf->tile_cfg;
72 const BITSTREAM_PROFILE profile = seq_params->profile;
73 const double level_bitrate_limit =
74 av1_get_max_bitrate_for_level(target_level, tier, profile);
75 const int64_t max_bitrate = (int64_t)(level_bitrate_limit * 0.70);
77#if !CONFIG_REALTIME_ONLY
93 int max_tiles, max_tile_cols;
94 av1_get_max_tiles_for_level(target_level, &max_tiles, &max_tile_cols);
95 while (tile_cfg->tile_columns > 0 &&
96 (1 << tile_cfg->tile_columns) > max_tile_cols) {
97 --tile_cfg->tile_columns;
99 const int tile_cols = (1 << tile_cfg->tile_columns);
100 while (tile_cfg->tile_rows > 0 &&
101 tile_cols * (1 << tile_cfg->tile_rows) > max_tiles) {
102 --tile_cfg->tile_rows;
106 const int still_picture = seq_params->still_picture;
107 const double min_cr =
108 av1_get_min_cr_for_level(target_level, tier, still_picture);
109 rc_cfg->
min_cr = AOMMAX(rc_cfg->
min_cr, (
unsigned int)(min_cr * 100));
112#if !CONFIG_REALTIME_ONLY
131 int q,
int maxq,
int minq) {
134 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
135 int force_recode = 0;
158static inline double av1_get_gfu_boost_projection_factor(
double min_factor,
161 double factor = sqrt((
double)frame_count);
162 factor = AOMMIN(factor, max_factor);
163 factor = AOMMAX(factor, min_factor);
164 factor = (200.0 + 10.0 * factor);
168static inline int get_gfu_boost_from_r0_lap(
double min_factor,
169 double max_factor,
double r0,
171 double factor = av1_get_gfu_boost_projection_factor(min_factor, max_factor,
173 const int boost = (int)rint(factor / r0);
177static inline double av1_get_kf_boost_projection_factor(
int frame_count) {
178 double factor = sqrt((
double)frame_count);
179 factor = AOMMIN(factor, 10.0);
180 factor = AOMMAX(factor, 4.0);
181 factor = (75.0 + 14.0 * factor);
185static inline int get_regulated_q_overshoot(
AV1_COMP *
const cpi,
186 int is_encode_stage,
int q_low,
187 int q_high,
int top_index,
189 const AV1_COMMON *
const cm = &cpi->
common;
200 while (q_regulated < q_low && retries < 10) {
211static inline int get_regulated_q_undershoot(
AV1_COMP *
const cpi,
212 int is_encode_stage,
int q_high,
213 int top_index,
int bottom_index) {
214 const AV1_COMMON *
const cm = &cpi->
common;
223 while (q_regulated > q_high && retries < 10) {
256 AV1_COMP *
const cpi,
int *
const loop,
int *
const q,
int *
const q_low,
257 int *
const q_high,
const int top_index,
const int bottom_index,
258 int *
const undershoot_seen,
int *
const overshoot_seen,
259 int *
const low_cr_seen,
const int loop_count) {
260 AV1_COMMON *
const cm = &cpi->
common;
267 if (rc->is_src_frame_alt_ref &&
271 const int min_cr = rc_cfg->
min_cr;
273 const double compression_ratio =
275 const double target_cr = min_cr / 100.0;
276 if (compression_ratio < target_cr) {
278 if (*q < rc->worst_quality) {
279 const double cr_ratio = target_cr / compression_ratio;
280 const int projected_q = AOMMAX(*q + 1, (
int)(*q * cr_ratio * cr_ratio));
281 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->
worst_quality);
282 *q_low = AOMMAX(*q, *q_low);
283 *q_high = AOMMAX(*q, *q_high);
287 if (*low_cr_seen)
return;
291 !is_stat_generation_stage(cpi)) {
295 av1_init_level_info(cpi);
299 const AV1LevelInfo *
const level_info = level_params->level_info[0];
300 const DECODER_MODEL *
const decoder_models = level_info->decoder_models;
301 const AV1_LEVEL target_level = level_params->target_seq_level_idx[0];
303 if (target_level < SEQ_LEVELS &&
304 decoder_models[target_level].status == DECODER_MODEL_OK) {
305 DECODER_MODEL_STATUS status = av1_decoder_model_try_smooth_buf(
308 if ((status == SMOOTHING_BUFFER_UNDERFLOW ||
309 status == SMOOTHING_BUFFER_OVERFLOW) &&
310 *q < rc->worst_quality) {
312 *q_low = AOMMAX(*q, *q_low);
313 *q_high = AOMMAX(*q, *q_high);
322 const int last_q = *q;
323 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
325 &frame_under_shoot_limit,
326 &frame_over_shoot_limit);
327 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
330 p_rc->this_key_frame_forced &&
334 const int64_t low_err_target = cpi->
ambient_err >> 1;
336#if CONFIG_AV1_HIGHBITDEPTH
350 if ((kf_err > high_err_target &&
352 (kf_err > low_err_target &&
355 *q_high = AOMMAX(*q - 1, *q_low);
358 *q = (int)((*q * high_err_target) / kf_err);
359 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
360 }
else if (kf_err < low_err_target &&
364 *q_low = AOMMIN(*q + 1, *q_high);
367 *q = (int)((*q * low_err_target) / kf_err);
368 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
372 *q = clamp(*q, *q_low, *q_high);
373 *loop = (*q != last_q);
377 if (
recode_loop_test(cpi, frame_over_shoot_limit, frame_under_shoot_limit, *q,
378 AOMMAX(*q_high, top_index), bottom_index)) {
389 const double q_val_high_current =
390 av1_convert_qindex_to_q(*q_high, cm->
seq_params->bit_depth);
391 const double q_val_high_new =
394 *q_high = av1_find_qindex(q_val_high_new, cm->
seq_params->bit_depth,
399 *q_low = AOMMIN(*q + 1, *q_high);
401 if (*undershoot_seen || loop_count > 2 ||
402 (loop_count == 2 && !frame_is_intra_only(cm))) {
405 *q = (*q_high + *q_low + 1) / 2;
406 }
else if (loop_count == 2 && frame_is_intra_only(cm)) {
407 const int q_mid = (*q_high + *q_low + 1) / 2;
408 const int q_regulated = get_regulated_q_overshoot(
409 cpi, 1, *q_low, *q_high, top_index, bottom_index);
412 *q = (q_mid + q_regulated + 1) / 2;
414 *q = get_regulated_q_overshoot(cpi, 1, *q_low, *q_high, top_index,
421 *q_high = AOMMAX(*q - 1, *q_low);
423 if (*overshoot_seen || loop_count > 2 ||
424 (loop_count == 2 && !frame_is_intra_only(cm))) {
426 *q = (*q_high + *q_low) / 2;
427 }
else if (loop_count == 2 && frame_is_intra_only(cm)) {
428 const int q_mid = (*q_high + *q_low) / 2;
429 const int q_regulated = get_regulated_q_undershoot(
430 cpi, 1, *q_high, top_index, bottom_index);
433 *q = (q_mid + q_regulated) / 2;
439 if (rc_cfg->
mode ==
AOM_CQ && q_regulated < *q_low) {
443 *q = get_regulated_q_undershoot(cpi, 1, *q_high, top_index,
455 *undershoot_seen = 1;
459 *q = clamp(*q, *q_low, *q_high);
462 *loop = (*q != last_q);
Declares top-level encoder structures and functions.
@ AOM_CQ
Definition aom_encoder.h:188
@ AOM_Q
Definition aom_encoder.h:189
void av1_rc_update_rate_correction_factors(AV1_COMP *cpi, int is_encode_stage, int width, int height)
Updates the rate correction factor linking Q to output bits.
Definition ratectrl.c:940
static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q, int maxq, int minq)
Function to test for conditions that indicate we should loop back and recode a frame.
Definition rc_utils.h:130
static void recode_loop_update_q(AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low, int *const q_high, const int top_index, const int bottom_index, int *const undershoot_seen, int *const overshoot_seen, int *const low_cr_seen, const int loop_count)
Called after encode_with_recode_loop() has just encoded a frame. This function works out whether we u...
Definition rc_utils.h:255
SequenceHeader * seq_params
Definition av1_common_int.h:992
int width
Definition av1_common_int.h:791
RefCntBuffer * cur_frame
Definition av1_common_int.h:848
CurrentFrame current_frame
Definition av1_common_int.h:770
int height
Definition av1_common_int.h:792
Main encoder configuration data structure.
Definition encoder.h:944
RateControlCfg rc_cfg
Definition encoder.h:966
Top level encoder structure.
Definition encoder.h:2907
RATE_CONTROL rc
Definition encoder.h:3119
int64_t ambient_err
Definition encoder.h:3088
SPEED_FEATURES sf
Definition encoder.h:3139
unsigned char gf_frame_index
Definition encoder.h:3170
AV1EncoderConfig oxcf
Definition encoder.h:2955
AV1_COMMON common
Definition encoder.h:2950
AV1_PRIMARY * ppi
Definition encoder.h:2911
YV12_BUFFER_CONFIG * source
Definition encoder.h:2968
SVC svc
Definition encoder.h:3467
Top level primary encoder structure.
Definition encoder.h:2600
AV1LevelParams level_params
Definition encoder.h:2735
TWO_PASS twopass
Definition encoder.h:2750
PRIMARY_RATE_CONTROL p_rc
Definition encoder.h:2755
int use_svc
Definition encoder.h:2770
GF_GROUP gf_group
Definition encoder.h:2720
The stucture of acummulated frame stats in the first pass.
Definition firstpass.h:43
double duration
Definition firstpass.h:148
RECODE_LOOP_TYPE recode_loop
Definition speed_features.h:421
Primary Rate Control parameters and status.
Definition ratectrl.h:313
int64_t bits_off_target
Definition ratectrl.h:573
int64_t maximum_buffer_size
Definition ratectrl.h:375
int64_t starting_buffer_level
Definition ratectrl.h:365
int64_t buffer_level
Definition ratectrl.h:552
int64_t optimal_buffer_level
Definition ratectrl.h:370
Rate Control parameters and status.
Definition ratectrl.h:134
int best_quality
Definition ratectrl.h:247
int this_frame_target
Definition ratectrl.h:145
int projected_frame_size
Definition ratectrl.h:150
int worst_quality
Definition ratectrl.h:243
Encoder rate control configuration parameters.
Definition encoder.h:514
int worst_allowed_q
Definition encoder.h:584
int over_shoot_pct
Definition encoder.h:579
int64_t maximum_buffer_size_ms
Definition encoder.h:533
unsigned int min_cr
Definition encoder.h:563
enum aom_rc_mode mode
Definition encoder.h:598
int64_t starting_buffer_level_ms
Definition encoder.h:523
int64_t target_bandwidth
Definition encoder.h:538
int64_t optimal_buffer_level_ms
Definition encoder.h:528
int cq_level
Definition encoder.h:593
HIGH_LEVEL_SPEED_FEATURES hl_sf
Definition speed_features.h:2117
Two pass status and control data.
Definition firstpass.h:425